import time

from selenium.webdriver.common.by import By

from common.utils import ForumDriver

class ForumResister:
    def __init__(self):
        self.url="http://127.0.0.1:58080/sign-up.html"
        self.driver=ForumDriver.driver
        self.driver.get(self.url)

    def _clear_forum(self):
        # 清除输入框
        input_box=["#username","#nickname","#password","#passwordRepeat"]
        for box in input_box:
            self.driver.find_element(By.CSS_SELECTOR,box).clear()
        # 取消勾选的同意协议
        check_box=self.driver.find_element(By.CSS_SELECTOR,"#policy")
        if check_box.is_selected():
            check_box.click()

    def RegisterSucc(self):
        self._clear_forum()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("wang"+str(int(time.time())))
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("w"+str(int(time.time())))
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("12")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("12")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()

        # 点击注册
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        # 验证是否跳转到了登录界面
        time.sleep(1)
        curUrl=self.driver.current_url
        assert curUrl=="http://127.0.0.1:58080/sign-in.html"
        # 截图
        ForumDriver.getScreenShot()


    def RegisterFail(self):
        # 测试各种注册失败的情况
        test_cases=[
            # (描述, 用户名, 昵称, 密码, 确认密码, 勾选协议, 预期错误元素)
            ("全都不填写","","","","",False,"#username"),
            ("空用户名","","w1","12","12",True,"#username"),
            ("空昵称","wangx","","12","12",True,"#nickname"),
            ("空密码","wangx","w1","","12",True,"#password"),
            ("空确认密码","wangx","w1","12","",True,"#passwordRepeat"),
            ("密码和确认密码不同","wangx","w1","12","123",True,"#passwordRepeat"),
            ("不勾选协议","wangx","w1","12","12",False,"#policy"),
            ("输入的数据太长","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","w1","12","12",True,"#username"),
            ("用户名已存在","zhangsan","111","12","12",True,"#username")
        ]

        for desc,username,nickname,password,passwordRepeat,policy,err_element in test_cases:
            self._clear_forum()
            # 对输入框
            if username:
                self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(username)
            if nickname:
                self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys(nickname)
            if password:
                self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys(password)
            if passwordRepeat:
                self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys(passwordRepeat)

            # 这一行 随便点击一个元素
            self.driver.find_element(By.CSS_SELECTOR,"#password_a > svg").click()
            # 对是否勾选同意条款
            if policy:
                self.driver.find_element(By.CSS_SELECTOR,"#policy").click()

            time.sleep(3)
            # 点击注册
            self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
            time.sleep(1)
            # 验证：如果注册失败，还是在注册界面
            curUrl=self.driver.current_url
            assert curUrl == "http://127.0.0.1:58080/sign-up.html"
            # 截图
            ForumDriver.getScreenShot()


# ForumResister().RegisterSucc()
# ForumResister().RegisterFail()
# ForumDriver.driver.quit()



















